/*
 * @Author: aum
 * @Date: 2021-03-01 14:35:06
 * @LastEditTime: 2021-08-29 23:27:38
 * @LastEditors: Please set LastEditors
 * @Description:
 * @FilePath: \vue3-hand-vant\src\router\index.ts
 */
import { createRouter, createWebHistory } from 'vue-router'
import RouterCommon from './common'
import RouterModule from './modules'
import { userStore } from '@/store/modules/user'

// 路由集合
export const routes = [...RouterCommon, ...RouterModule]

// 创建路由
const router = createRouter({
  history: createWebHistory('./'),
  routes: routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userInfo = userStore.getUserInfoState
  if ((userInfo && to.meta.requiresAuth) || !to.meta.requiresAuth) {
    to.meta.title && (document.title = to.meta.title as string)
    next()
  } else {
    userStore.dispatchUserLogout() // 需要授权但是没有获取到用户信息, 跳转登录页面
  }
})

export default router
